/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mlu.jdbc.simple.codegeneration;

import java.io.IOException;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.mlu.jdbc.simple.SimpleJdbc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mlungisi Sincuba
 * @since 1.0
 */
public class DaoClassGenerator {
    private DataSource dataSource;
    private static final Logger logger = LoggerFactory.getLogger(DaoClassGenerator.class);

    public DaoClassGenerator(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     * @return the dataSource
     */
    public DataSource getDataSource() {
        return dataSource;
    }

    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    
     public void generateDaoInterface(String src, String packageName, boolean mavenProject, String[] tableNames) throws SQLException, IOException {
        src = CodeUtil.getSrcPath(src, mavenProject);
        packageName += ".dao";
        String packagePath = CodeUtil.getPackagePath(src, packageName);
        
        String[] tableNamesArray = tableNames;
        tableNamesArray = CodeUtil.toTablesArray(tableNames, tableNamesArray, getDataSource());
        for (String tableName : tableNamesArray) {
            logger.debug("TableName is {}", tableName);
            
            StringBuilder sb = new StringBuilder("package ").append(packageName).append(";\n\n");
            sb.append("import org.mlu.jdbc.simple.dao.GenericDao;\n\n");
            String interfaceName = CodeUtil.interfaceDeclaration(tableName, sb);
            CodeUtil.saveTofile(packagePath, interfaceName, sb);
        }
     }
    
    public static void main(String... args) throws SQLException, IOException {
        SimpleJdbc jdbc = new SimpleJdbc();
        DomainClassGenerator domainClassGenerator = new DomainClassGenerator(jdbc.getDataSource());
        domainClassGenerator.generateDomain(null, "com.movirtu", true, null);
        DaoClassGenerator classGenerator = new DaoClassGenerator(jdbc.getDataSource());
        classGenerator.generateDaoInterface(null, "com.movirtu", true, null);
    }
}
